package com.kevin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class Ex2 {
    @RequestMapping("plugin/labourCompetition/NpsCompetitionScore_mobile_uploading")
    @ResponseBody
    public JsonData importTemplate(@RequestParam(value = "file") MultipartFile file) throws Exception {
        JsonData jsonData = new JsonData();

        Boolean bool = ImportExcel.checkFile(file);
        if(!bool){
            jsonData.setStatus("文件类型不正确或为空");
            return jsonData;
        }
        //工具类在下面
        HashMap<String, ArrayList<String[]>> hashMap = ImportExcel.analysisFile(file);
        ArrayList<String[]> arrayList = hashMap.get("OK");
        if(arrayList == null){
            Set<String> strings = hashMap.keySet();
            String next = strings.iterator().next();
            jsonData.setStatus(next);
            return jsonData;
        }


        //数据都在arrayList中，循环入库，记得最后一个字段加上loadTime
//        ConnectSettings dwConnect = commUtil.getDwConnect();
        String sql = " INSERT INTO 表名  values ";

        String nowDate = ImportExcel.getNowDate();
        for(int n = 0;n < arrayList.size();n++){
            String[] str = arrayList.get(n);
            sql += " ( ";
            for(int num =0;num<str.length;num++){
                sql += "'" +str[num]+"',";
                if(num == str.length-1){
                    sql += "to_date('"+nowDate+"','yyyy-mm-dd hh24:mi:ss')";
                }
            }
            if( n != arrayList.size()-1 ){
                sql += " ),";
            }else{
                sql += " )";
            }
        }

        System.err.println(sql);

//        int i = DbHelper.executeNonQuery(sql, dwConnect);
//        System.err.println("成功执行行数"+i);
//
//        if(i < 1){
//            jsonData.setStatus("导入失败");
//            return jsonData;
//        }
        jsonData.setStatus("导入成功");
        return jsonData;
    }
}
